<template>
  <div class="AddUserHelp">

    <el-dialog title="申请帮助" :visible.sync="dialogHelpFormVisible" :center="isCenter" width="500px">
      <el-form :model="dataForm" ref="dataForm" :rules="roleRules" label-width="150px">
        <el-form-item label="医检名称" prop="institutionId">
          <el-select
            v-model="dataForm.institutionId"
            filterable
            clearable
            reserve-keyword
            placeholder="请选择医检"
          >
            <el-option
              v-for="item in pickInstitutionList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <OfficeSelect @officeId="getOfficeId"
                      :institution-id="dataForm.institutionId" ></OfficeSelect>
        <el-form-item label="医生级别" prop="doctorLevel">
          <el-select v-model="dataForm.doctorLevel" placeholder="请选择医生级别">
            <el-option
              v-for="item in doctorLevelList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帮助类型" prop="adviceFlag">
          <el-select v-model="dataForm.adviceFlag" placeholder="请选择帮助类型">
            <el-option
              v-for="item in adviceFlagList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帮助内容" prop="content">
          <el-input type="textarea" v-model="dataForm.content" autocomplete="off" :rows="5" :cols="30"></el-input>
        </el-form-item>
        <el-form-item align="center">
          <el-button @click.native="clickCancel">取 消</el-button>
          <el-button type="primary" @click.native="clickCommit">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import UserApi from '@/api/user/user';
  import InstitutionApi from "@/api/institution/institution";
  import OfficeSelect from "@/components/OfficeSelect";

  export default {
    name: 'AddUserHelp',
    props: {
      relationId: {
        type: Number,
        default: null
      }
    },
    created() {
      this.listInstitution()
    },
    watch:{
      relationId(value) {
        this.dataForm.relationId = value
      }
    },
    data() {
      return {
        dataForm:{
          relationId:"",
          institutionId:"",
          officeId:"",
          doctorLevel:"",
          adviceFlag:"",
          adviceType: 3,
          content:""
        },
        doctorLevelList: [
          { label: '主任', value: '1' },
          { label: '副主任', value: '2' },
          { label: '医师', value: '3' }
        ],
        adviceFlagList: [
          { label: '报告单解读', value: '1' },
          { label: '诊疗指导', value: '2' }
        ],
        pickInstitutionList: [],
        dialogHelpFormVisible: false,
        isCenter: true,
        roleRules: {
          adviceFlag:[
            { required: true, message: "请选择帮助类型", trigger: "change" }
          ],
          content: [
            { required: true, message: "请输入帮助内容", trigger: "blur" },
            { max: 200, message: '内容长度不超过200个字', trigger: 'blur' }
          ],
        }
      }
    },
    components: {
      OfficeSelect
    },
    methods: {
      listInstitution () {
        let _this = this
        let cond = {
        }
        InstitutionApi.selectInsByUser(cond)
          .then(res => {
            setTimeout(() => {
              _this.loadingIns = false
              _this.pickInstitutionList = res.data.map(item => {
                return {
                  value: item.institutionId,
                  label: item.institutionName
                }
              })
            }, 200)
          })
          .catch(error => {
            _this.pickInstitutionList = []
            console.error(error)
          })
      },
      getOfficeId(val) {
        this.dataForm.officeId = val
      },
      clickCancel(){
        this.dialogHelpFormVisible = false;
        this.dataForm.adviceFlag="";
        this.dataForm.content="";
        this.dataForm.relationId="";
      },
      clickCommit() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.addData();
           /* this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {})
              .then(() => {

              });*/
          }
        })
      },
      addData(){
        let _this = this;
        UserApi.addUserAdvice(this.dataForm)
          .then((res) =>{
            _this.dialogHelpFormVisible = false;
            _this.$Utils.notifySuccess()
          })
      },
      handleCreate() {
        this.dialogHelpFormVisible = true
      }

    }
  }
</script>
<style lang="scss">
  .variant-input {
    margin-top: 2px
  }

  .AddUserHelp .country_ul {
    position: relative;
  }

  .country_ul .country_tabs {
    float: left;
    margin-bottom: 0;
    padding-left: 0;
    list-style: none;
    margin-bottom: -4px;
  }

  .country_ul .country_tabs li {
    float: left;
    cursor: pointer;
    background-color: #eee;
    width: 60px;
    height: 35px;
    margin-right: 5px;
    border: 1px solid #ddd;
    border-radius: 6px;
  }

  .country_ul .country_tabs .active {
    background-color: #fff;
  }

  .country_ul .country_tabs li a {
    span {
      line-height: 30px;
      margin-left: 15px;
    }
  }

  .variant_btn {
    display: flex;
    justify-content: center;
    margin-top: 10px;
  }
</style>
